//Jen Anderson
//CS 362
//random test quiz

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>

//set NOISY_TEST to 0 to remove print output
#define NOISY_TEST 1 

/********************************************************************************
*										*
*  This function needs to randomly generate all the possible characters		*
*  Need all the characters generated for character 32 through 126		*
*										*
********************************************************************************/

char inputChar()
{
	//generates random numbers 32 to 126 and then casts them to a char
	return (char)(rand() % 95 + 32);
}

/********************************************************************************
*										*
*  This function needs to create random strings of length 5 with a '\0'		*
*  character at the end.  Each position in the string will have a possibility   *
*  to have any of the 26 lower case letters in the position	 		*
*										*
********************************************************************************/

char *inputString()
{ 
	int i;
	//Create a new string of size 6
	char *s = (char*)malloc(6);
	//For each of the first 5 characters
	for(i = 0; i < 5; i++)
  		//the character can be any of the characters 97 to 122
	      	s[i] = (char)(rand() % 26 + 97);
    	//the last character is empty 
	s[5] = '\0';
    	return s;
}

void testme()
{
  	int tcCount = 0;
  	char *s;
  	char c;
  	int state = 0;
  	while (1)
	{
    		tcCount++;
		c = inputChar();
    		s = inputString();
		
		if(NOISY_TEST == 1)
		{ 
    			printf("Iteration %d: c = %c, s = %s, state = %d\n", tcCount, c, s, state);
		}

    		if (c == '[' && state == 0) state = 1;
    		if (c == '(' && state == 1) state = 2;
    		if (c == '{' && state == 2) state = 3;
    		if (c == ' '&& state == 3) state = 4;
    		if (c == 'a' && state == 4) state = 5;
    		if (c == 'x' && state == 5) state = 6;
    		if (c == '}' && state == 6) state = 7;
    		if (c == ')' && state == 7) state = 8;
    		if (c == ']' && state == 8) state = 9;
    		if 
			(s[0] == 'r' && s[1] == 'e'
       			&& s[2] == 's' && s[3] == 'e'
       			&& s[4] == 't' && s[5] == '\0'
       			&& state == 9)
		{	
      			printf("error \n");
			if(NOISY_TEST == 0)
			{
				printf("The test went through %d iteratations before producing the error.\n", tcCount);
			}
      			exit(0);
    		}
  	}
}

int main(int argc, char *argv[])
{
    srand(time(NULL));
    testme();
    return 0;
}

